package edu.gmu.projects.fireresponder.gui;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import edu.gmu.projects.fireresponder.R;

public class NotificationsList extends ListActivity {
	public static final String MESSAGE_BODY = "MESSAGE_BODY";
	private NotificationsDbAdapter mDbHelper;
	private Cursor mNotificationsCursor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notification_list);
		mDbHelper = new NotificationsDbAdapter(this);
		mDbHelper.open();
		// use this for example to load the database with some data
		// createNote();

		fillData();
		registerForContextMenu(getListView());

		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		Log.i("NotificationsList", "Created NotificationsList ListActivity");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		
		if (mNotificationsCursor != null)
			mNotificationsCursor.close();

		if (mDbHelper != null)
			mDbHelper.close();

		Log.i("NotificationsList", "Destroyed NotificationsList ListActivity");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.notification_list_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection

		switch (item.getItemId()) {
		case R.id.situation_map_menu:
			startActivity(new Intent(this, SituationMap.class));
			return true;
		case R.id.clear_notes:
			mDbHelper.clearDb();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	/*
	private void createNote() {
		Intent i = new Intent(this, NotificationsContent.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}
	*/
	
	private void fillData() {
		// Get all of the notifications from the database and create the item
		// list
		mNotificationsCursor = mDbHelper.fetchAllNotes();
		startManagingCursor(mNotificationsCursor);

		String[] from = new String[] { NotificationsDbAdapter.KEY_TITLE };
		int[] to = new int[] { R.id.text1 };

		// Now create an array adapter and set it to display using our row
		SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
				R.layout.notifications_row, mNotificationsCursor, from, to);
		setListAdapter(notes);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Cursor note = mDbHelper.fetchNote(id);
		startManagingCursor(note);

		String value = note.getString(note
				.getColumnIndexOrThrow(NotificationsDbAdapter.KEY_BODY));

		Intent intent = new Intent(getApplicationContext(),
				NotificationsListItemActivity.class);
		//intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(NotificationsList.MESSAGE_BODY, value);

		startActivity(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		fillData();
	}
}